from fastapi import APIRouter, Request

from app.database.postgre import SessionDep
from app.models.api import (CardLoginJSON, CardHeartbeatParams, CardLogoutJSON, CardUnbindJSON,
                            CardRechargeJSON, CardInfoParams, AppFlagAndTimeParams, AppCheckUpdateParams,
                            TrialLoginJSON, TrialHeartbeatParams, UpdateExtraDataJSON)
from app.services import api

router = APIRouter(
    prefix='/api/v1',
    tags=['develop', 'api']
)


@router.post('/card/login')
async def card_login(json_data: CardLoginJSON, session: SessionDep, request: Request):
    app = await api.check_request_sign(json_data, session, request)
    await api.check_firewall_rule(app, session, request, device_code=json_data.device_code)
    response = await api.handle_card_login(app, json_data, session, request)
    return response


@router.get('/card/heartbeat')
async def card_heartbeat(query_params: CardHeartbeatParams, session: SessionDep, request: Request):
    app = await api.check_request_sign(query_params, session, request)
    await api.check_firewall_rule(app, session, request, device_code=query_params.device_code)
    response = await api.handle_card_heartbeat(app, query_params, session, request)
    return response


@router.post('/card/logout')
async def card_logout(json_data: CardLogoutJSON, session: SessionDep, request: Request):
    app = await api.check_request_sign(json_data, session, request)
    await api.check_firewall_rule(app, session, request, device_code=json_data.device_code)
    response = await api.handle_card_logout(json_data)
    return response


@router.post('/card/unbind')
async def card_unbind(json_data: CardUnbindJSON, session: SessionDep, request: Request):
    app = await api.check_request_sign(json_data, session, request)
    await api.check_firewall_rule(app, session, request, device_code=json_data.device_code)
    response = await api.handle_card_unbind(app, json_data, session)
    return response


@router.post('/card/recharge')
async def card_recharge(json_data: CardRechargeJSON, session: SessionDep, request: Request):
    app = await api.check_request_sign(json_data, session, request)
    await api.check_firewall_rule(app, session, request)
    response = await api.handle_card_recharge(app, json_data, session, request)
    return response


@router.get('/card/info')
async def card_info(query_params: CardInfoParams, session: SessionDep, request: Request):
    app = await api.check_request_sign(query_params, session, request)
    await api.check_firewall_rule(app, session, request)
    response = await api.get_card_info(app, query_params, session)
    return response


@router.get('/app/config')
async def app_config(query_params: AppFlagAndTimeParams, session: SessionDep, request: Request):
    app = await api.check_request_sign(query_params, session, request)
    await api.check_firewall_rule(app, session, request)
    response = await api.get_app_config(app)
    return response


@router.get('/app/notice')
async def app_notice(query_params: AppFlagAndTimeParams, session: SessionDep, request: Request):
    app = await api.check_request_sign(query_params, session, request)
    await api.check_firewall_rule(app, session, request)
    response = await api.get_app_notice(app)
    return response


@router.get('/app/extra_data')
async def app_extra_data(query_params: AppFlagAndTimeParams, session: SessionDep, request: Request):
    app = await api.check_request_sign(query_params, session, request)
    await api.check_firewall_rule(app, session, request)
    response = await api.get_app_extra_data(app)
    return response


@router.get('/app/check_update')
async def app_check_update(query_params: AppCheckUpdateParams, session: SessionDep, request: Request):
    app = await api.check_request_sign(query_params, session, request)
    await api.check_firewall_rule(app, session, request)
    response = await api.check_app_update(app, query_params, session)
    return response


@router.post('/trial/login')
async def trial_login(json_data: TrialLoginJSON, session: SessionDep, request: Request):
    app = await api.check_request_sign(json_data, session, request)
    await api.check_firewall_rule(app, session, request, device_code=json_data.device_code)
    response = await api.handle_trial_login(app, json_data, session, request)
    return response


@router.get('/trial/heartbeat')
async def trial_heartbeat(query_params: TrialHeartbeatParams, session: SessionDep, request: Request):
    app = await api.check_request_sign(query_params, session, request)
    await api.check_firewall_rule(app, session, request, device_code=query_params.device_code)
    response = await api.handle_trial_heartbeat(app, query_params, session)
    return response


@router.post('/card/extra_data')
async def update_card_extra_data(json_data: UpdateExtraDataJSON, session: SessionDep, request: Request):
    app = await api.check_request_sign(json_data, session, request)
    await api.check_firewall_rule(app, session, request, device_code=json_data.device_code)
    response = await api.handle_update_card_extra_data(app, json_data, session)
    return response
